package com.ktjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.pages.ProjectPage;
import com.ktjy.pojo.BasDict;
import com.ktjy.pojo.CstCustomer;
import com.ktjy.pojo.SalChance;
import com.ktjy.pojo.SysUser;
import com.ktjy.pojo.vo.SysUserVo;
import com.ktjy.service.BasDictService;
import com.ktjy.service.CstCustomerService;
import com.ktjy.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 营销机会管理
 */
@Controller
public class CstCustomerController {

    @Autowired
    CstCustomerService cstCustomerService;
    @Autowired
    BasDictService basDictService;
    @Autowired
    SysUserService sysUserService;


    @RequestMapping("/customer/list")
    public Object customerList(CstCustomer cstCustomer, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "2") Integer pageSize, Model model) {
        IPage<CstCustomer> cstCustomerIPage = new Page<>(pageIndex, pageSize);
        QueryWrapper<CstCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.hasText(cstCustomer.getCustName()), CstCustomer::getCustName, cstCustomer.getCustName())
                .eq(StringUtils.hasText(cstCustomer.getCustRegion()), CstCustomer::getCustRegion, cstCustomer.getCustRegion());
        Page<CstCustomer> page = (Page<CstCustomer>) cstCustomerService.page(cstCustomerIPage, queryWrapper);
        ProjectPage<CstCustomer> projectPage = new ProjectPage<>();
        BeanUtils.copyProperties(page, projectPage);
        QueryWrapper<BasDict> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(BasDict::getDictType, "地区");
        model.addAttribute("page", projectPage);
        model.addAttribute("dictList", basDictService.list(queryWrapper1));
        model.addAttribute("custName", cstCustomer.getCustName());
        model.addAttribute("custRegion", cstCustomer.getCustRegion());
        return "/customer/list";
    }

    @RequestMapping("/customer/add")
    public Object customerAdd(Model model) {
//        model.addAttribute("users", sysUserService.list());
        return "/chance/add";
    }
    @RequestMapping("/customer/del/{custNo}")
    @ResponseBody
    public Object chanceDel(@PathVariable String custNo) {
        QueryWrapper<CstCustomer> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda().eq(CstCustomer::getCustNo, custNo);
        Map<String,Object> map= new HashMap<>();
        boolean flg = cstCustomerService.remove(queryWrapper);
        String delResult = null;
        if (flg){
            delResult = "true";
        }else {
            delResult = "false";
        }
        map.put("delResult",delResult );
        return map;
    }

    @RequestMapping("/customer/edit")
    public Object chanceEdit(String customercId,Model model) {
        //查询公司（客户名称）
        model.addAttribute("customer", cstCustomerService.getById(customercId));
        //查询所属地区
        QueryWrapper<BasDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BasDict::getDictType, "地区");
        model.addAttribute("areas", basDictService.list(queryWrapper));
        //查询客户等级
        QueryWrapper<BasDict> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().eq(BasDict::getDictType, "客户等级");
        model.addAttribute("customerLevel", basDictService.list(queryWrapper2));
        //查询负责人
        QueryWrapper<SysUserVo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(SysUserVo::getUsrRoleId,2).or().eq(SysUserVo::getUsrRoleId, 33);
        model.addAttribute("users", sysUserService.list(queryWrapper1));
        return "/customer/edit";
    }

    @RequestMapping("/customer/save")
    public Object chanceSave(CstCustomer cstCustomer) {
        if (cstCustomer != null) {
            boolean flg = cstCustomerService.saveOrUpdate(cstCustomer);
            if (flg) {
                return "redirect:/customer/list";
            } else {
                return "/customer/add";
            }
        } else {
            return "/customer/add";
        }
    }
}
